import Vue from 'vue'
import VueRouter, { RouteConfig } from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

  const routes: Array<RouteConfig> = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    redirect:'/home/firstpage',
    children:[
      {
        path: '/home/firstpage',
        name: 'firstpage',
        component: () => import('../views/firstpage.vue'),
        meta:({
          reqirePath:true,
          title:'多点超市'
        })
      },{
        path: '/home/classify',
        meta:{title:'分类'},
        name: 'classify',
        component: () => import('../views/classify.vue')
      },{
        path: '/home/shopcar',
        meta:{title:'购物车'},
        name: 'shopcar',
        component: () => import('../views/shopcar.vue')
      },{
        path: '/home/my',
        meta:{title:'我的'},
        name: 'my',
        component: () => import('../views/my.vue')
      },
    
    ],
  
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/user/login.vue'),
    meta:{title:'登录'},
  },{
    path: '/register',
    name: 'register',
    component: () => import('../views/user/register.vue'),
    meta:{title:'注册'},
  },{
    path: '/forget',
    name: 'forget',
    component: () => import('../views/user/forget.vue'),
  },{
    path: '/serch',
    name: 'serch',
    component: () => import('../views/serch.vue'),
    meta:{title:'搜索'},
  },
  {
    path: '/detal/:id',
    name: 'detal',
    component: () => import('../views/detal.vue'),
    meta:{title:'商品详情'},
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,form,next)=>{
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
  if(to.meta.reqirePath){
    if(localStorage.getItem("key")){
      next()
    }else{
      next({
        path:"/login",
      })
    }
  }else{
    next()
  }
})

export default router
